<template>
  <view class="xl-grid">
    <view v-for="(group, groupIndex) in groups" :key="groupIndex" :class="['xl-grid-group', border && 'xl-grid-border']"
          :style="{justifyContent: group.length < col ? (align == 'left' ? 'flex-start': align == 'right' ? 'flex-end' : 'center') : 'space-between'}">
      <template v-for="(item, index) in group" :key="index">
        <view class="xl-grid-item" :style="{'--col': col, '--divider': divider ? (col-1)+'px' : '0%', height: $xl.calcUnit(height, 'rpx')}" @click="clickItem(item)">
          <slot :item="item">
            <view class="xl-grid-item-icon" :style="{'--icon-size': $xl.calcUnit(iconSize, 'px')}">
              <xl-icon :family="item.family" :name="item.icon" :color="iconColor" :size="iconSize"></xl-icon>
              <view v-if="item.dot" class="dot"></view>
              <view v-if="item.badge" class="badge">{{item.badge}}</view>
            </view>
            <view :style="{fontSize: $xl.calcUnit(fontSize, 'px'), color}">{{item.name}}</view>
          </slot>
        </view>
        <xl-line v-if="divider && index < group.length -1" direction="col" length="30"></xl-line>
      </template>
    </view>
  </view>
</template>

<script setup>
import {ref, computed, watch, onMounted} from 'vue'
import propsData from './props.js'

const props = defineProps(propsData)
const emits = defineEmits(['click'])

const groups = ref([])

watch(() => props.items, () => {
  groups.value = []
  for (let i = 0; i < props.items.length; i += props.col) groups.value.push(props.items.slice(i, i + props.col));
}, { immediate: true })

function clickItem(item) {
  emits('click', item)
  // 如果配置了url参数，跳转页面
  if (item.url) {
    uni.$xl.router.push({name: item.url, params: item.params})
  }
}

</script>

<style lang="scss" scoped>
.xl-grid {
  background: v-bind(background);

  .xl-grid-group {
    display: flex;
    flex-direction: row;
    align-items: center;
    justify-content: space-between;

    &.xl-grid-border {
      border-bottom: 1rpx solid #efefef;
    }

    .xl-grid-item {
      width: calc((100% - var(--divider)) / var(--col));
      height: var(--height);
      display: flex;
      flex-direction: column;
      justify-content: center;
      align-items: center;

      &-icon {
        position: relative;
        width: calc(var(--icon-size) + 4px);
        height: calc(var(--icon-size) + 4px);
        display: flex;
        justify-content: center;
        align-items: center;

        .dot {
          position: absolute;
          right: -1px;
          top: 0;
          width: 7px;
          height: 7px;
          border-radius: 50%;
          background: #f56c6c;
        }

        .badge {
          position: absolute;
          right: -4px;
          top: 0;
          width: 12px;
          height: 12px;
          line-height: 13px;
          border-radius: 50%;
          background: #f56c6c;
          color: #ffffff;
          font-size: 18rpx;
          text-align: center;
        }
      }
    }
  }
}
</style>